<?php

require_once  $_SERVER['DOCUMENT_ROOT']."/../_lib/base/common.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.hotels.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.destinations.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.exchange_rates.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.photos.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.bookings.php";


// Start Sessions
session_start();

// check for search results and booking timeouts (see: common.php)
if($location = check_timeouts()) {
  header("Location: $location");
  exit;
}



$results = $_SESSION["hotels_search_results"]["data"];
$booking_details = $_SESSION["booking"];
	
switch($_POST['action']){
	default:
            if(count($_POST)) {
                    // Delete any old $_SESSION variables relating to previous bookings
                    unset($_SESSION['booking']);

                $results = $_SESSION["hotels_search_results"]["data"];
                $hotel_id = intval($_POST["hotel_id"]);
                $hotel = $results["hotels"][$hotel_id];

                // save booking details
                $room_ids = array();
                // read in room ids
                $i = 1;  
                $total = 0;
                while(isset($_POST["room_id_".$i])) {
                    $room_ids[$i] = $_POST["room_id_".$i];
                    $total += $hotel["room_sets"][$i]["rooms"][$room_ids[$i]]["Rate"];
                    $i++;
                }
                // store booking details
                $_SESSION["booking"]["hotel_id"] = $hotel_id;
                $_SESSION["booking"]["hotel"] = $hotel;
                $_SESSION["booking"]["room_ids"] = $room_ids;
                $_SESSION["booking"]["total"] = $total;  
            }
            else {
                $hotel = $_SESSION["booking"]["hotel"];
                $hotel_id = $_SESSION["booking"]["hotel_id"];
                $room_ids = $_SESSION["booking"]["room_ids"];
              //  $booking_details = $_SESSION["booking"]["details"];
            }

                        //$template->ld_head();
                        // Show booking page
                        include SITE_ROOT_PATH."/_lib/html/booking.php";
                        //$template->ld_foot();
	break;
	case "validate";
            $hotel = $_SESSION["booking"]["hotel"];
            $hotel_id = $_SESSION["booking"]["hotel_id"];
            $room_ids = $_SESSION["booking"]["room_ids"];
            $booking_details = $_SESSION["booking"]["details"];

            $bookings = new bookings();
                    $form_errors = $bookings->validate_booking_form($_POST);
                    if(!empty($form_errors)){
                            $booking_details = $_POST;
                            $my_message = array();
                            $my_message['type'] = 'neg';
                            $my_message['message'] = "There was a problem with the form";
                            $my_message['list'] = $form_errors;
                            $GLOBALS['note'] = $my_message;
                            // Show booking page
                            //$template->ld_head();
                            include SITE_ROOT_PATH."/_lib/html/booking.php";
                            //$template->ld_foot();
                    }else{
        // store booking details, including timestamp to track data lifetime
                        $_SESSION["booking"]["details"] = $_POST;
                        $_SESSION["booking"]["details"]["total"] = $_SESSION["booking"]["total"];
                        $_SESSION["booking"]["details"]["total_currency"] = $hotel["currency"];
                        //$_SESSION["booking"]["details"]["timestamp"] = time();
                                // redirect browser to search results page
                                // avoid form being resubmitted repeat when refreshing browser
                                // save posted values and redirect to next page
                        header("Location: booking_payment.php");
                        exit;
                    }
	break;
}






